package com.itheima.service.impl;


import com.itheima.bean.exception.BusinessException;
import com.itheima.bean.pojo.User;
import com.itheima.bean.vo.*;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//业务层
@Service//当前类对象由Springboot的IOC容器管理
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    /**
     * 分页查询用户数据
     *
     * @param pagination 分页对象
     * @return
     */
    public PageBean queryUserByPage(Pagination pagination) {
        if (pagination == null) {
            throw new BusinessException(Code.BUSINESS_ERR,"分页参数为空");
        }
        Integer currentPage = pagination.getCurrentPage();
        Integer pageSize = pagination.getPageSize();
        if (currentPage == null) {
            currentPage = 1;
        }


        if (pageSize == null) {
            pageSize = 5;
        }


        //计算起始索引
        Integer index = (currentPage - 1) * pageSize;
        //调用dao层功能,分页查询用户信息
        List<User> userList = userDao.findUserByPage(index, pageSize);
        //调用dao层功能：查询总记录数
        int rowcount = userDao.findCount();

        //分页查询bean对象
        PageBean<User>pageBean= null;
        if (userList != null && !userList.isEmpty()){
            pageBean = new PageBean<>();
            pageBean.setRows(userList);//封装查询的结果
            pageBean.setTotal(rowcount);//封装总行数
        }


        return pageBean;
    }


    /**
     * 添加用户及角色
     *
     * @param user
     * @return
     */
    @Override
    public boolean addUserAndRole(AddUser user) {
        if (user == null) {
            throw new BusinessException(Code.BUSINESS_ERR,"用户参数为空");
        }
        //功能1：添加用户
        boolean saveResult = saveUser(user);

        //功能2：添加用户角色 (要使用到新增用户的id)
        if (saveResult){
            saveUserRole(user.getId(),user.getRoleIds());
        }
        return true;
    }


    @Override
    public boolean deleteUser(Integer id) {
        boolean resultRole = userDao.deleteUserRoleById(id);
        boolean result = userDao.deleteUser(id);
        if (resultRole && result){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateUserRole(UpdateUser user) {
        //1.更新用户
        updateUser(user);
        //2.删除中间表
        deleteUser_Role(user);
        //3.添加用户方法
        addUserRole(user);
        return true;
    }
    //更新用户表
    private void addUserRole(UpdateUser user) {
        Integer id = user.getId();
        List<String> roleIds = user.getRoleIds();

        for (String roleId : roleIds) {
            int IntegerRoleId = Integer.parseInt(roleId);
            userDao.addUserRole(id,IntegerRoleId);
        }
    }
   //删除中间表
    private void deleteUser_Role(UpdateUser user) {
        Integer uid = user.getId();
        userDao.deleteUser_Role(uid);

    }

    private void updateUser(UpdateUser user) {userDao.updateUser(user);
    }

    public  boolean saveUser (AddUser user){
        //添加dao层功能：添加用户
        int result = userDao.addUser(user);
        return result > 0? true : false ;
    }

    public void saveUserRole(Integer userid,List<String> roleIds){
        //循环：添加用户角色
        for (String roleid : roleIds) {
            //调用dao层功能：添加用户角色
            userDao.addUserRole(userid,Integer.parseInt(roleid));
        }
    }
}
